package com.bootdo.xzl.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.dao.DictDao;
import com.bootdo.common.domain.DictDO;
import com.bootdo.common.elasticsearch.BaseElasticService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.service.UserService;
import com.bootdo.xzl.dao.HousesInfoDao;
import com.bootdo.xzl.domain.BuildingManagementDO;
import com.bootdo.xzl.domain.HousesImgInfoDO;
import com.bootdo.xzl.service.BuildingManagementService;

/**
 * 精选写字楼管理
 * 
 * @author chenhuan
 * @email huan.chen@i-soft.com.cn
 * @date 2019-12-16 09:42:12
 */

@Controller
@RequestMapping("/xzl/buildingManagement")
public class BuildingManagementController {

	@Autowired
	private BuildingManagementService buildingManagementService;

	@Autowired
	private RestHighLevelClient restHighLevelClient;
	@Autowired
	private BaseElasticService baseElasticService;
	@Autowired
	private DictDao dictDao;
	@Autowired
	UserService userService;
	@Autowired
	private HousesInfoDao housesInfoDao;

	@GetMapping()
	@RequiresPermissions("xzl:buildingManagement:buildingManagement")
	String BuildingManagement() {
		return "xzl/buildingManagement/buildingManagement";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("xzl:buildingManagement:buildingManagement")
	public PageUtils list(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);
		List<BuildingManagementDO> buildingManagementList = buildingManagementService.list(query);
		int total = buildingManagementService.count(query);
		PageUtils pageUtils = new PageUtils(buildingManagementList, total);
		return pageUtils;
	}

	@ResponseBody
	@GetMapping("/es/managementList")
	// @RequiresPermissions("xzl:buildingManagement:buildingManagement")
	public PageUtils managementList(@RequestParam Map<String, Object> params) {

		BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

		QueryBuilder matchAll = QueryBuilders.matchAllQuery();
		//boolQueryBuilder.must(matchAll);
		boolQueryBuilder.filter(matchAll);
	     //删除区分
		boolQueryBuilder.filter(QueryBuilders.matchQuery("del_flag", "0"));
	      //类别
		boolQueryBuilder.filter(QueryBuilders.matchQuery("type", "management"));
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

//		searchSourceBuilder.sort("id", SortOrder.DESC);
		searchSourceBuilder.sort("office_ranking", SortOrder.DESC);
		searchSourceBuilder.sort("office_kbn", SortOrder.DESC);
		searchSourceBuilder.sort("create_date", SortOrder.DESC);
		searchSourceBuilder.query(boolQueryBuilder);

		SearchRequest request = new SearchRequest("management");
		request.source(searchSourceBuilder);

		List<BuildingManagementDO> buildingManagementList = null;

        //判断所以是否为空
		PageUtils pageUtils = new PageUtils(null, 0);
	
		buildingManagementList = baseElasticService.search("management", searchSourceBuilder, BuildingManagementDO.class);
	   pageUtils.setRows(buildingManagementList);
		return pageUtils;
	}

	@ResponseBody
	@GetMapping("/es/managementListall")
	// @RequiresPermissions("xzl:buildingManagement:buildingManagement")
	public List<BuildingManagementDO> managementListall(@RequestParam Map<String, Object> params) {

		BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

		QueryBuilder matchAll = QueryBuilders.matchAllQuery();
		boolQueryBuilder.must(matchAll);
		//boolQueryBuilder.filter(matchAll);
	     //删除区分
		boolQueryBuilder.filter(QueryBuilders.matchQuery("del_flag", "0"));
	      //类别
		//boolQueryBuilder.filter(QueryBuilders.matchQuery("type", "management"));
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		searchSourceBuilder.from(0);
		searchSourceBuilder.sort("id", SortOrder.DESC);
		searchSourceBuilder.size(10000);
		searchSourceBuilder.query(boolQueryBuilder);
		
		
		
		
		
		
		
		
		
		
		
		
//		searchSourceBuilder.sort("id", SortOrder.DESC);
		
//		searchSourceBuilder.sort("office_ranking", SortOrder.DESC);
//		searchSourceBuilder.sort("office_kbn", SortOrder.DESC);
//		searchSourceBuilder.sort("create_date", SortOrder.DESC);
	    //searchSourceBuilder.query(boolQueryBuilder);

		//SearchRequest request = new SearchRequest("management");
		//request.source(searchSourceBuilder);

		List<BuildingManagementDO> buildingManagementList = null;

		
		buildingManagementList = baseElasticService.search("management", searchSourceBuilder, BuildingManagementDO.class);
		return buildingManagementList;
	}

	// 详情接口
	@ResponseBody
	@GetMapping("/es/getbuilding")
	// @RequiresPermissions("xzl:housesInfo:housesInfo")
	public BuildingManagementDO getbuilding(@RequestParam("id") Integer id) {
		BuildingManagementDO buildingManagementDO = new BuildingManagementDO();
		BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

		QueryBuilder matchAll = QueryBuilders.matchAllQuery();

		boolQueryBuilder.filter(matchAll);
	     //ID
		boolQueryBuilder.filter(QueryBuilders.matchQuery("id", id));
	     //删除区分
		boolQueryBuilder.filter(QueryBuilders.matchQuery("del_flag", "0"));

	      //类别
		boolQueryBuilder.filter(QueryBuilders.matchQuery("type", "management"));
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

//		searchSourceBuilder.sort("id", SortOrder.DESC);
		searchSourceBuilder.sort("office_ranking", SortOrder.DESC);
		searchSourceBuilder.sort("office_kbn", SortOrder.DESC);
		searchSourceBuilder.sort("create_date", SortOrder.DESC);
		searchSourceBuilder.query(boolQueryBuilder);

		
		List<BuildingManagementDO> buildingManagementList  =new ArrayList<BuildingManagementDO>() ;
		buildingManagementList = baseElasticService.search("management", searchSourceBuilder, BuildingManagementDO.class);
		buildingManagementDO =buildingManagementList.get(0);
		
		
		BoolQueryBuilder boolQueryBuilderImg = QueryBuilders.boolQuery();

		QueryBuilder matchAllImg = QueryBuilders.matchAllQuery();

		boolQueryBuilderImg.filter(matchAllImg);
	     //ID
		boolQueryBuilderImg.filter(QueryBuilders.matchQuery("office_id", id));
	     //删除区分
		boolQueryBuilderImg.filter(QueryBuilders.matchQuery("del_flag", "0"));
	    //删除区分
		boolQueryBuilder.filter(QueryBuilders.matchQuery("img_flg", "2"));
	      //类别
		boolQueryBuilderImg.filter(QueryBuilders.matchQuery("type", "images"));
		SearchSourceBuilder searchSourceBuilderImg = new SearchSourceBuilder();
		searchSourceBuilderImg.query(boolQueryBuilderImg);
		
		List<HousesImgInfoDO> housesImgInfoLst  =new ArrayList<HousesImgInfoDO>() ;
		housesImgInfoLst = baseElasticService.search("images", searchSourceBuilderImg, HousesImgInfoDO.class);
		buildingManagementDO.setHousesImgInfoList(housesImgInfoLst);
		
		// 用户信息
		UserDO userDO = userService.getuser(Long.valueOf(buildingManagementDO.getCreateBy()));
		if(userDO!=null) {
			buildingManagementDO.setImgNo(userDO.getHeadPortraitPath());		
		}
	
		return buildingManagementDO;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("xzl:buildingManagement:add")
	String add(Model model) {

		// 用户信息
		UserDO user = ShiroUtils.getUser();
		model.addAttribute("user", user);

		// 火热等级
		Map<String, Object> officeKbnParam = new HashMap<>();
		officeKbnParam.put("type", "officeKbn");
		List<DictDO> officeKbnList = dictDao.list(officeKbnParam);
		model.addAttribute("officeKbnList", officeKbnList);

		// 等级
		Map<String, Object> officeRankingParam = new HashMap<>();
		officeRankingParam.put("type", "officeRanking");
		List<DictDO> officeRankingList = dictDao.list(officeRankingParam);
		model.addAttribute("officeRankingList", officeRankingList);

		return "xzl/buildingManagement/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("xzl:buildingManagement:edit")
	String edit(@PathVariable("id") Integer id, Model model) {

		BuildingManagementDO buildingManagement = buildingManagementService.get(id);
		model.addAttribute("buildingManagement", buildingManagement);

		// 用户信息
		UserDO user = ShiroUtils.getUser();
		model.addAttribute("user", user);

		// 火热登记
		Map<String, Object> officeKbnParam = new HashMap<>();
		officeKbnParam.put("type", "officeKbn");
		officeKbnParam.put("value", buildingManagement.getOfficeKbn());
		List<DictDO> officeKbnList = housesInfoDao.dictListByValue(officeKbnParam);
		model.addAttribute("officeKbnList", officeKbnList);

		// 等级
		Map<String, Object> officeRankingParam = new HashMap<>();
		officeRankingParam.put("type", "officeRanking");
		officeRankingParam.put("value", buildingManagement.getOfficeRanking());
		List<DictDO> officeRankingList = housesInfoDao.dictListByValue(officeRankingParam);
		model.addAttribute("officeRankingList", officeRankingList);

		return "xzl/buildingManagement/edit";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("xzl:buildingManagement:add")
	public R save(BuildingManagementDO buildingManagement) {
	  
		if (buildingManagementService.save(buildingManagement) > 0) {
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("xzl:buildingManagement:edit")
	public R update(BuildingManagementDO buildingManagement) {
		buildingManagementService.update(buildingManagement);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ResponseBody
	@RequiresPermissions("xzl:buildingManagement:remove")
	public R remove(Integer id) {
		BuildingManagementDO param = new BuildingManagementDO();
		param.setId(id);
		param.setDelFlag("1");
		if (buildingManagementService.update(param) > 0) {
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@PostMapping("/batchRemove")
	@ResponseBody
	@RequiresPermissions("xzl:buildingManagement:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids) {
		buildingManagementService.batchUpdate(ids);
		return R.ok();
	}

}
